#!/bin/sh
DOVECOT_HOME=/var/run/dovecot/login
SRVDIR=/var/lib/dovecot
if [ ! -d "$DOVECOT_HOME" ] ; then
  mkdir -p "$(dirname $DOVECOT_HOME)"
  busybox addgroup -S dovecot
  busybox adduser -h "$DOVECOT_HOME" -s /bin/nologin -S -D -G dovecot dovecot
  busybox addgroup -S dovenull
  busybox adduser -h "$DOVECOT_HOME" -s /bin/nologin -S -D -G dovenull dovenull
  install -D -d -m 755 "$SRVDIR"
  chown -R dovecot "$SRVDIR"
  chgrp -R dovecot "$SRVDIR"
fi
if [ ! -f /etc/dovecot/dovecot.key ] ; then
  openssl req -new -x509 -days 3650 -nodes -out /etc/dovecot/dovecot.crt \
     -subj '/C=US/ST=Oregon/L=Portland/CN=invalid.org' \
     -keyout /etc/dovecot/dovecot.key && chmod o= /etc/dovecot/dovecot.key || exit 1
  chgrp -R dovecot /etc/dovecot/
  chmod -R 640 /etc/dovecot/
fi
[ "$1" = "--prereqs" ] && exit 0
exec dovecot -F 2>&1
